package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.html.EscapeUtil;
import com.ruoyi.common.utils.security.Md5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TCandidateContactMapper;
import com.ruoyi.system.domain.TCandidateContact;
import com.ruoyi.system.service.ITCandidateContactService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

/**
 * 考生联系信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class TCandidateContactServiceImpl implements ITCandidateContactService 
{
    @Autowired
    private TCandidateContactMapper tCandidateContactMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TCandidateContactServiceImpl.class);

    /**
     * 查询考生联系信息
     * 
     * @param contactId 考生联系信息主键
     * @return 考生联系信息
     */
    @Override
    public TCandidateContact selectTCandidateContactByContactId(Long contactId)
    {
        return tCandidateContactMapper.selectTCandidateContactByContactId(contactId);
    }

    /**
     * 查询考生联系信息列表
     * 
     * @param tCandidateContact 考生联系信息
     * @return 考生联系信息
     */
    @Override
    public List<TCandidateContact> selectTCandidateContactList(TCandidateContact tCandidateContact)
    {
        return tCandidateContactMapper.selectTCandidateContactList(tCandidateContact);
    }

    /**
     * 新增考生联系信息
     * 
     * @param tCandidateContact 考生联系信息
     * @return 结果
     */
    @Override
    public int insertTCandidateContact(TCandidateContact tCandidateContact)
    {
        tCandidateContact.setCreateTime(DateUtils.getNowDate());
        return tCandidateContactMapper.insertTCandidateContact(tCandidateContact);
    }

    /**
     * 修改考生联系信息
     * 
     * @param tCandidateContact 考生联系信息
     * @return 结果
     */
    @Override
    public int updateTCandidateContact(TCandidateContact tCandidateContact)
    {
        tCandidateContact.setUpdateTime(DateUtils.getNowDate());
        return tCandidateContactMapper.updateTCandidateContact(tCandidateContact);
    }

    /**
     * 批量删除考生联系信息
     * 
     * @param contactIds 需要删除的考生联系信息主键
     * @return 结果
     */
    @Override
    public int deleteTCandidateContactByContactIds(String contactIds)
    {
        return tCandidateContactMapper.deleteTCandidateContactByContactIds(Convert.toStrArray(contactIds));
    }

    /**
     * 删除考生联系信息信息
     * 
     * @param contactId 考生联系信息主键
     * @return 结果
     */
    @Override
    public int deleteTCandidateContactByContactId(Long contactId)
    {
        return tCandidateContactMapper.deleteTCandidateContactByContactId(contactId);
    }
    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importData(List<TCandidateContact> userList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.isEmpty())
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TCandidateContact user : userList)
        {
            try
            {
                // 验证是否存在这个联系方式id
                if (user.getContactId() == null)
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    user.setUpdateTime(DateUtils.getNowDate());
                    user.setCreateBy(operName);
                    user.setCreateTime(DateUtils.getNowDate());
                    tCandidateContactMapper.insertTCandidateContact(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    user.setUpdateTime(DateUtils.getNowDate());
                    tCandidateContactMapper.updateTCandidateContact(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() +  " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String loginName = "1";
                if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class))
                {
                    loginName = EscapeUtil.clean(loginName);
                }
                String msg = "<br/>" + failureNum + "、账号 " + loginName + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
